 package cn.com.cal;
 
 import cn.com.common.Poke;
 //import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.List;
// import java.util.Vector;
 
 
 /**
  * 牌值加工
  * @author Administrator
  *
  */
 public class PrudentialCal
 {
	 
	 /**
	  * 从高到底排序
	  * @param array
	  */
	 public static void sortAry(List<Poke> array)
	 {
		 for (int i = 0; i < array.size(); ++i) 
		 {
			 int max = i;
			 for (int j = i + 1; j < array.size(); ++j) 
			 {
				 if (((Poke)array.get(j)).getValue() > ((Poke)array.get(max)).getValue()) 
				 {
					 max = j;
			     }
             }
		       if (max != i) 
		       {
		    	   Poke temp = (Poke)array.get(i);
		    	   array.set(i, (Poke)array.get(max));
		    	   array.set(max, temp);
		       }
        }
     }
 
	 
   public static PrudentialVo countSame(List<Poke> array)
   {
	     int nCount = 0;
	     int start = 0;
	     int dcount = 0;
	     int threcount = 0;
	     int fourcount = 0;
	     int index = -1;
	     //Vector<Byte> vector = new Vector<Byte>();
 
     for (int i = 1; i < array.size(); ++i)
     {
	       if (nCount == 0)
	       {
	         nCount = 1;
	         start = ((Poke)array.get(i - 1)).getValue();
	       }
	       
	       if (start == ((Poke)array.get(i)).getValue()) 
	       {
		         ++nCount;
		         index = i - 1;
		 
		         if (nCount == 2) 
		         {
		        	 ++dcount;
		         }
	        }
	        else if (nCount == 3) 
	        {
	        	threcount = 1;
	        } else if (nCount == 4) 
	        {
	        	fourcount = 1;
	        } else 
	        {
	        	nCount = 0;
	        }
     }
 
     if ((array.size() < 4) && 
       (nCount == 2)) 
     {
    	 dcount = 1;
     }
 
     if (nCount == 3) 
     {
	       threcount = 1;
	       if (array.size() == 4) 
	       {
	         --index;
	       }
     }
     if (nCount == 4) 
     {
    	 fourcount = 1;
     }
     if ((array.size() == 3) && (dcount != 0) && (index != 0)) 
     {
	       Poke pp = (Poke)array.get(0);
	       array.remove(0);
	       array.add(pp);
     }
     if ((array.size() == 4) && (dcount == 1) && (index != 0)) 
     {
	       if (index == 1) 
	       {
	         array.add(3, (Poke)array.get(0));
	         array.remove(0);
	       }
	       if (index == 2) 
	       {
	         array.add((Poke)array.get(0));
	         array.add((Poke)array.get(1));
	         array.remove(0);
	         array.remove(0);
	       }
     }
     else if ((array.size() == 4) && (threcount == 1) && (index != 0)) 
     {
    	 array.add((Poke)array.get(0));
    	 array.remove(0);
     }
     PrudentialVo vo = new PrudentialVo();
     vo.setDcount(dcount);
     vo.setThreecount(threcount);
     vo.setFourcount(fourcount);
     vo.setVisablepoke(array);
 
     return vo;
   }
 
   public static PrudentialVo cal_cards(List<Poke> array)
   {
     sortAry(array);
     return countSame(array);
   }
 
   public static void main(String[] args)
   {
     List array = new ArrayList();
// 
//     Poke poke1 = new Poke(260);
//     Poke poke2 = new Poke(514);
//     Poke poke3 = new Poke(1026);
//     Poke poke4 = new Poke(772);
 
//     array.add(poke1);
//     array.add(poke2);
//     array.add(poke3);
//     array.add(poke4);
// 
//     PrudentialVo vector = cal_cards(array);
 
     System.out.println();
     for (int j = 0; j < array.size(); ++j)
       System.out.print(Integer.toHexString(((Poke)array.get(j)).getFlag()) + "\t");
   }
 }

